package com.blkj.iam.system.controller;

import com.blkj.iam.common.result.ApiResult;
import com.blkj.iam.core.security.util.SecurityUtils;
import com.blkj.iam.system.mapper.UserRoleMapper;
import com.blkj.iam.system.model.entity.Dept;
import com.blkj.iam.system.model.entity.Menu;
import com.blkj.iam.system.model.entity.Role;
import com.blkj.iam.system.model.entity.User;
import com.blkj.iam.system.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据同步前端控制层
 *
 * @author Theo
 * @since 2024-07-30 11:25
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "10.系统配置")
@RequestMapping("/api/v1/sync")
public class SyncDataController {

    private final UserService userService;
    private final RoleService roleService;
    private final DeptService deptService;
    private final MenuService menuService;
    private final UserRoleMapper userRoleMapper;
    private final ServerDataSyncService dataSyncService;

    @Operation(summary = "用户新增")
    @GetMapping("/add/user")
    public ApiResult addUser(@PathVariable Long id) {
        User user = userService.getById(id);
        Long tenantId = SecurityUtils.getTenantId();
        List<Long> roleIdList = userRoleMapper.getRoleIdList(tenantId, id);
        dataSyncService.addUser(user, roleIdList);
        return ApiResult.OK();
    }

    @Operation(summary = "用户编辑")
    @GetMapping("/edit/user")
    public ApiResult editUser(@PathVariable Long id) {
        User user = userService.getById(id);
        Long tenantId = SecurityUtils.getTenantId();
        List<Long> roleIdList = userRoleMapper.getRoleIdList(tenantId, id);
        dataSyncService.editUser(user, roleIdList);
        return ApiResult.OK();
    }

    @Operation(summary = "用户删除")
    @GetMapping("/delete/user")
    public ApiResult delUser(@PathVariable Long id) {
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        dataSyncService.deleteUsers(ids);
        return ApiResult.OK();
    }

    @Operation(summary = "角色新增")
    @GetMapping("/add/role")
    public ApiResult addRole(@PathVariable Long id) {
        Role role = roleService.getById(id);
        dataSyncService.addRole(role);
        return ApiResult.OK();
    }

    @Operation(summary = "角色边界")
    @GetMapping("/edit/role")
    public ApiResult editRole(@PathVariable Long id) {
        Role role = roleService.getById(id);
        dataSyncService.editRole(role);
        return ApiResult.OK();
    }

    @Operation(summary = "角色新增")
    @GetMapping("/delete/role")
    public ApiResult delRole(@PathVariable Long id) {
        Role role = roleService.getById(id);
        dataSyncService.deleteRole(role);
        return ApiResult.OK();
    }

    @Operation(summary = "部门新增")
    @GetMapping("/add/dept")
    public ApiResult addDept(@PathVariable Long id) {
        Dept dept = deptService.getById(id);
        dataSyncService.addDept(dept);
        return ApiResult.OK();
    }

    @Operation(summary = "部门编辑")
    @GetMapping("/edit/dept")
    public ApiResult editDept(@PathVariable Long id) {
        Dept dept = deptService.getById(id);
        dataSyncService.editDept(dept);
        return ApiResult.OK();
    }

    @Operation(summary = "部门删除")
    @GetMapping("/delete/dept")
    public ApiResult delDept(@PathVariable Long id) {
        Dept dept = deptService.getById(id);
        dataSyncService.deleteDept(dept.getIdStr());
        return ApiResult.OK();
    }

    @Operation(summary = "菜单新增")
    @GetMapping("/add/menu")
    public ApiResult addMenu(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        dataSyncService.addMenu(menu);
        return ApiResult.OK();
    }

    @Operation(summary = "菜单编辑")
    @GetMapping("/edit/menu")
    public ApiResult editMenu(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        dataSyncService.editMenu(menu);
        return ApiResult.OK();
    }

    @Operation(summary = "菜单新增")
    @GetMapping("/delete/menu")
    public ApiResult delMenu(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        dataSyncService.deleteMenu(menu.getIdStr());
        return ApiResult.OK();
    }
}
